package com.guo.controller;

import com.guo.service.BatchService;
import com.guo.util.ExportTextUtil;
import com.guo.util.MsgBaseRtn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther Heart Guo
 * @Date 2022/03/08-14:29
 */
@RestController
@RequestMapping("/user")
public class BatchAddController {

    @Autowired
    private BatchService batchService;

    /**
     * 批量上传
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("batchAdd")
    @ResponseBody
    public MsgBaseRtn batchAdd(MultipartFile file, HttpServletRequest request){
        return batchService.batchAdd(file, request);
    }

    /**
     * 错误日志导出
     * @param request
     * @param response
     */
    @RequestMapping("/exportError")
    public void export(HttpServletRequest request, HttpServletResponse response){
        String str = request.getSession().getAttribute("error_batch_add_text").toString();
        ExportTextUtil.writeToTxt(response, str, "数据导入错误日志" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    }
}
